package com.workcell.workdata.xthings.constant;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.function.Function;

/**
 * 串口类型枚举
 */
@Getter
@RequiredArgsConstructor
public enum SerialCommunicationTypeEnum {
    /**
     * 有线串口
     */
    SERIAL_ETHERNET("有线网口串口服务器", SerialTopicUtil::generatePublishTopicEthernet, SerialTopicUtil::generateSubscribeTopicEthernet),
    /**
     * 4G串口
     */
    SERIAL_4G("无线4G串口服务器", SerialTopicUtil::generatePublishTopic4G, SerialTopicUtil::generateSubscribeTopic4G);

    /**
     * 通讯模式
     */
    private final String hardwareName;
    /**
     * 构建发布主题的方法
     */
    private final Function<String, String> publishTopicFun;
    /**
     * 构建订阅主题的方法
     */
    private final Function<String, String> subscribeTopicFun;

    public static String generatePublishTopic(String deviceNo, String hardwareName) {
        return Arrays.stream(SerialCommunicationTypeEnum.values())
                .filter(v -> StrUtil.equals(v.getHardwareName(), hardwareName))
                .map(v -> v.publishTopicFun.apply(deviceNo))
                .findFirst()
                .orElse("");
    }

    public static String generateSubscribeTopic(String deviceNo, String hardwareName) {
        return Arrays.stream(SerialCommunicationTypeEnum.values())
                .filter(v -> StrUtil.equals(v.getHardwareName(), hardwareName))
                .map(v -> v.subscribeTopicFun.apply(deviceNo))
                .findFirst()
                .orElse("");
    }
}
